package com.home766.controller.admin;

import com.home766.entity.Dictionary;
import com.home766.service.IDictionaryService;
import net.biz.Message;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.controller.admin.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/dictionary")
public class DictionaryController extends BaseController {

	@Autowired
	private IDictionaryService dictionaryServiceImpl;
	
	
	
	
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(String title, String description, Boolean flag, ModelMap model, Pageable pageable){
		Map<String, Object> params=new HashMap<String,Object>();
		Page<Dictionary> page= dictionaryServiceImpl.searchPage(params,pageable);
		model.addAttribute("page",page);
		return "admin/dictionary/list";
	}
	/**
	 * search roots
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/searcheAll",method=RequestMethod.GET)
	public String searchAll(ModelMap model){
		List<Dictionary> list= dictionaryServiceImpl.searchRoots();
		model.addAttribute("roots",list);
		return "admin/dictionary/list";
	}
	
	/**
	 * jump add
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String add(ModelMap model){
		String str=dictionaryServiceImpl.searchAll(null);
		model.addAttribute("nodes", str);
		return "admin/dictionary/add";
	}
	/**
	 * jump edit
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.GET)
	public String edit(Long id, ModelMap model){
		Dictionary dictionary=dictionaryServiceImpl.find(id);
		String str=dictionaryServiceImpl.searchAll(id);
		model.addAttribute("nodes", str);
		model.addAttribute("dictionary", dictionary);
		return "admin/dictionary/edit";
	}
	@RequestMapping(value="/show",method=RequestMethod.GET)
	public String show(Long id, ModelMap model){
		Dictionary dictionary=dictionaryServiceImpl.find(id);
		model.addAttribute("dictionary", dictionary);
		return "admin/dictionary/show";
	}
	/**
	 * save dictioanry
	 * @param dictionary
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String add(String title, String description, Boolean flag, Long parentId, Integer order, ModelMap model, RedirectAttributes redirectAttributes){
		//判断是否有相同的名称
		Dictionary dictionary=new Dictionary();
		if(null!=parentId){
			Dictionary parent= dictionaryServiceImpl.find(parentId);
			dictionary.setParent(parent);
		}
		dictionary.setTitle(title);
		dictionary.setChildrens(null);
		dictionary.setDescription(description);
		dictionary.setFlag(flag);
		dictionary.setOrder(order);
		dictionaryServiceImpl.save(dictionary);
//		staticService.buildDictionary(0);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}
	
	/**
	 * update dictioanry
	 * @param dictionary
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(Long id, String title, String description, Boolean flag, Long parentId, Integer order, ModelMap model, RedirectAttributes redirectAttributes){
		if(id!=null){
			Dictionary dictionary=dictionaryServiceImpl.find(id);
			Dictionary parent=dictionaryServiceImpl.find(parentId);
			dictionary.setParent(parent);
			dictionary.setTitle(title);
			dictionary.setDescription(description);
			dictionary.setFlag(flag);
			dictionary.setOrder(order);
			dictionaryServiceImpl.update(dictionary);
			addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		}else{
			addFlashMessage(redirectAttributes, ERROR_MESSAGE);
		}
		return "redirect:list.jhtml";
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Message delete(Long[] ids) {
		dictionaryServiceImpl.delete(ids);
//		if("success".equals(type)){
//			staticService.buildDictionary(0);
			return SUCCESS_MESSAGE;
//		}
//		return Message.warn("admin.message.error.dictionary.type",result.get("msg"));
	}
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/del")
	public Message del(Long id) {
		dictionaryServiceImpl.delete(id);
		return SUCCESS_MESSAGE;
	}
}
